var $global_rootpath;
var $global_rootpath_static;
var $global_rootpath_front;
var $global_isMobile,$global_isIPad,$global_isIPhone,$global_isIPod,$global_isAndroid;
var $global_winWidth,$global_winHeight;
var appId = "";
//js获取项目根路径，如： http://localhost:8083/uimcardprj
function getRootPath(){
    //获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp
    var curWwwPath=window.document.location.href;
    //获取主机地址之后的目录，如： uimcardprj/share/meun.jsp
    var pathName=window.document.location.pathname;
    var pos=curWwwPath.indexOf(pathName);
    //获取主机地址，如： http://localhost:8083
    var localhostPaht=curWwwPath.substring(0,pos);
    //获取带"/"的项目名，如：/uimcardprj
    var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
    var basePath = localhostPaht;
    if(projectName.indexOf("ieventvm")>=0){
    	basePath +=  projectName;
    }
    return basePath;
}

$(function(){
	$( "#popup-outside-page" ).enhanceWithin().popup();
	$( "[data-role='header'], [data-role='footer']" ).toolbar();
	
	//设置全局变量
	$global_rootpath = getRootPath();
	$global_rootpath_static = $global_rootpath + "/static";
	$global_rootpath_front = $global_rootpath_static + "/front/mobile";
	
	//获取手机类型
	$global_isMobile = navigator.userAgent.match( /(iPad)|(iPhone)|(iPod)|(Android)|(PlayBook)|(BB10)|(BlackBerry)|(Opera Mini)|(IEMobile)|(webOS)|(MeeGo)/i );
	$global_isIPad = navigator.userAgent.match( /(iPad)/i );
	$global_isIPhone = navigator.userAgent.match( /(iPhone)/i );
	$global_isIPod = navigator.userAgent.match( /(iPod)/i );
	$global_isAndroid = navigator.userAgent.match( /(Android)/i );
	$global_winWidth = $( window ).width();
	$global_winHeight = $( window ).height();
	
	//导航栏图片尺寸设置
	showHeadIcon();
	if(appId.length>0){
		$(".ui-page-active").css("padding-top",0);
		$(".ui-page-active").css("padding-bottom",0);
		if($(".more-menu").length>0) $(".more-menu").css("top", 0);
		if($(".more-menu2").length>0) $(".more-menu2").css("top", 0);
	}
	
});

//显示加载器  
function showLoader(text) {  
    //显示加载器.for jQuery Mobile 1.2.0  
    $.mobile.loading('show', {  
        text: text, //加载器中显示的文字  
        textVisible: true, //是否显示文字  
        theme: 'b',        //加载器主题样式a-e  
        textonly: false,   //是否只显示文字  
        html: ""           //要显示的html内容，如图片等  
    });  
} 

function loading(text) {  
    //显示加载器.for jQuery Mobile 1.2.0  
    $.mobile.loading('show', {  
        text: text, //加载器中显示的文字  
        textVisible: true, //是否显示文字  
        theme: 'b',        //加载器主题样式a-e  
        textonly: false,   //是否只显示文字  
        html: ""           //要显示的html内容，如图片等  
    });  
}

function showLoader() {  
    //显示加载器.for jQuery Mobile 1.2.0  
    $.mobile.loading('show', {  
        text: '加载中...', //加载器中显示的文字  
        textVisible: true, //是否显示文字  
        theme: 'b',        //加载器主题样式a-e  
        textonly: false,   //是否只显示文字  
        html: ""           //要显示的html内容，如图片等  
    });  
}
  
//隐藏加载器.for jQuery Mobile 1.2.0  
function hideLoader()  
{  
    //隐藏加载器  
    $.mobile.loading('hide');  
}

function openPopup(){
	$("#popup").popup("open");
}

function openPopup(text){
	$("#popup p").text(text);
	$("#popup").popup("open");
}

function closePopup(time){
	setTimeout(function(){
	   $("#popup").popup("close");
	}, time);//两秒后关闭
}

//上移
function arrowUp(obj)
{
 	var onthis = $(obj).parent();
 	var getup = $(obj).parent().prev();
 	if(getup.get(0) != undefined){
	 	var onthis_sort = onthis.find("#sort").val();
	 	var getup_sort = getup.find("#sort").val();
	 	//排序号重新赋值
	 	onthis.find("#sort").val(getup_sort);
	 	getup.find("#sort").val(onthis_sort);
 	}
 	$(getup).before(onthis);
}
//下移
function arrowDown(obj)
{
 	var onthis = $(obj).parent();
 	var getdown = $(obj).parent().next();
 	if(getdown.get(0) != undefined){
	 	var onthis_sort = onthis.find("#sort").val();
	 	var getdown_sort = getdown.find("#sort").val();
	 	//排序号重新赋值
	 	onthis.find("#sort").val(getdown_sort);
	 	getdown.find("#sort").val(onthis_sort);
 	}
 	$(getdown).after(onthis);
}
//高级搜索面板
function showSearchPanel(){
	$(".search-panel").width($global_winWidth);
	$(".search_bg").show();
	$(".search-panel").stop().animate({bottom: "0"}, 500);
}
function closeSearchPanel(){
	$(".search-panel").stop().animate({bottom: "-100%"}, 500);
	$(".search_bg").hide();
}

function changeTwoDecimal(x){
	var f_x = parseFloat(x);
	if (isNaN(f_x)){
		alert('转换出错');
		return false;
	}
	f_x = Math.round(f_x *100)/100;
	return f_x;
}

function changeTwoDecimal_f(x){
	var f_x = parseFloat(x);
	if (isNaN(f_x))
	{
		alert('转换出错');
		return false;
	}
	f_x = Math.round(f_x*100)/100;
	var s_x = f_x.toString();
	var pos_decimal = s_x.indexOf('.');
	if (pos_decimal < 0)
	{
	pos_decimal = s_x.length;
		s_x += '.';
	}
	while (s_x.length <= pos_decimal + 2)
	{
		s_x += '0';
	}
	return s_x;
}

function showHeadIcon(){
	if($global_isAndroid){
		if($(".head-back-icon").length>0) {
			$(".head-back-icon").attr("src", $global_rootpath_front + "/images/head/android/back.png");
			$(".head-back-icon").css("width", "51px").css("height", "20px");
		}
		if($(".head-close-icon").length>0) {
			$(".head-close-icon").attr("src", $global_rootpath_front + "/images/head/android/close.png");
			$(".head-close-icon").css("width", "28px").css("height", "14px");
		}
		if($(".head-search-icon").length>0) {
			$(".head-search-icon").attr("src", $global_rootpath_front + "/images/head/android/search.png");
			$(".head-search-icon").css("width", "26px").css("height", "26px");
		}
		if($(".head-more-icon").length>0) {
			$(".head-more-icon").attr("src", $global_rootpath_front + "/images/head/android/more.png");
			$(".head-more-icon").css("width", "30px");
		}
		if($(".head-share-icon").length>0) {
			$(".head-share-icon").attr("src", $global_rootpath_front + "/images/head/android/share.png");
			$(".head-share-icon").css("width", "28px").css("height", "14px");
		}
		if($(".head-add-icon").length>0) {
			$(".head-add-icon").attr("src", $global_rootpath_front + "/images/head/android/add.png");
			$(".head-add-icon").css("width", "30px").css("height", "15px");
		}
		if($(".head-save-icon").length>0) {
			$(".head-save-icon").attr("src", $global_rootpath_front + "/images/head/android/save.png");
			$(".head-save-icon").css("width", "30px").css("height", "15px");
		}
		if($(".head-save_sort-icon").length>0) {
			$(".head-save_sort-icon").attr("src", $global_rootpath_front + "/images/head/android/save_sort.png");
			$(".head-save_sort-icon").css("width", "60px").css("height", "15px");
		}
		if($(".head-edit-icon").length>0) {
			$(".head-edit-icon").attr("src", $global_rootpath_front + "/images/head/android/edit.png");
			$(".head-edit-icon").css("width", "30px").css("height", "15px");
		}
	}else if($global_isIPad || $global_isIPhone || $global_isIPod){
		if($(".head-back-icon").length>0) {
			$(".head-back-icon").attr("src", $global_rootpath_front + "/images/head/ios/back.png");
			$(".head-back-icon").css("width", "48px").css("height", "24px");
		}
		if($(".head-close-icon").length>0) {
			$(".head-close-icon").attr("src", $global_rootpath_front + "/images/head/ios/close.png");
			$(".head-close-icon").css("width", "28px").css("height", "14px");
		}
		if($(".head-search-icon").length>0) {
			$(".head-search-icon").attr("src", $global_rootpath_front + "/images/head/ios/search.png");
			$(".head-search-icon").css("width", "26px").css("height", "26px");
		}
		if($(".head-more-icon").length>0) {
			$(".head-more-icon").attr("src", $global_rootpath_front + "/images/head/ios/more.png");
			$(".head-more-icon").css("width", "30px");
		}
		if($(".head-share-icon").length>0) {
			$(".head-share-icon").attr("src", $global_rootpath_front + "/images/head/ios/share.png");
			$(".head-share-icon").css("width", "28px").css("height", "14px");
		}
		if($(".head-add-icon").length>0) {
			$(".head-add-icon").attr("src", $global_rootpath_front + "/images/head/ios/add.png");
			$(".head-add-icon").css("width", "30px").css("height", "15px");
		}
		if($(".head-save-icon").length>0) {
			$(".head-save-icon").attr("src", $global_rootpath_front + "/images/head/ios/save.png");
			$(".head-save-icon").css("width", "30px").css("height", "15px");
		}
		if($(".head-save_sort-icon").length>0) {
			$(".head-save_sort-icon").attr("src", $global_rootpath_front + "/images/head/ios/save_sort.png");
			$(".head-save_sort-icon").css("width", "60px").css("height", "15px");
		}
		if($(".head-edit-icon").length>0) {
			$(".head-edit-icon").attr("src", $global_rootpath_front + "/images/head/ios/edit.png");
			$(".head-edit-icon").css("width", "30px").css("height", "15px");
		}
	}else{
		if($(".head-back-icon").length>0) {
			$(".head-back-icon").attr("src", $global_rootpath_front + "/images/head/android/back.png");
			$(".head-back-icon").css("width", "51px").css("height", "20px");
		}
		if($(".head-close-icon").length>0) {
			$(".head-close-icon").attr("src", $global_rootpath_front + "/images/head/android/close.png");
			$(".head-close-icon").css("width", "28px").css("height", "14px");
		}
		if($(".head-search-icon").length>0) {
			$(".head-search-icon").attr("src", $global_rootpath_front + "/images/head/android/search.png");
			$(".head-search-icon").css("width", "26px").css("height", "26px");
		}
		if($(".head-more-icon").length>0) {
			$(".head-more-icon").attr("src", $global_rootpath_front + "/images/head/android/more.png");
			$(".head-more-icon").css("width", "30px");
		}
		if($(".head-share-icon").length>0) {
			$(".head-share-icon").attr("src", $global_rootpath_front + "/images/head/android/share.png");
			$(".head-share-icon").css("width", "28px").css("height", "14px");
		}
		if($(".head-add-icon").length>0) {
			$(".head-add-icon").attr("src", $global_rootpath_front + "/images/head/android/add.png");
			$(".head-add-icon").css("width", "30px").css("height", "15px");
		}
		if($(".head-save-icon").length>0) {
			$(".head-save-icon").attr("src", $global_rootpath_front + "/images/head/android/save.png");
			$(".head-save-icon").css("width", "30px").css("height", "15px");
		}
		if($(".head-save_sort-icon").length>0) {
			$(".head-save_sort-icon").attr("src", $global_rootpath_front + "/images/head/android/save_sort.png");
			$(".head-save_sort-icon").css("width", "60px").css("height", "15px");
		}
		if($(".head-edit-icon").length>0) {
			$(".head-edit-icon").attr("src", $global_rootpath_front + "/images/head/android/edit.png");
			$(".head-edit-icon").css("width", "30px").css("height", "15px");
		}
	}
}

function initBodySize(){
	var headHeight = $(".ui-header")?$(".ui-header").height():0;
	var footHeight = $(".ui-footer")?$(".ui-footer").height():0;
	var contentHeight = $global_winHeight - headHeight - footHeight;
	$(".custom-content").height(contentHeight);
}

function onComplete(ul){ //手机端滑动排序完成处理函数
	var num=$(ul).nextAll().length, max=$(".sortable li").length;
	if(num==0){ //排在最后一个
		var prevSort = $(ul).prev().find("input[name='sorts']").val();
		$(ul).find("input[name='sorts']").val(parseInt(prevSort)+5);
	}else if(num==max-1){ //排在第一个
		var nextSort = $(ul).next().find("input[name='sorts']").val();
		$(ul).find("input[name='sorts']").val(5);  //第一个默认赋值5
		$(".sortable li").each(function(index){
			if(index>0){ //后面的每个自动加5
				var sortObj = $(this).find("input[name='sorts']");
				sortObj.val(parseInt(sortObj.val())+5);
			}
		});
	}else{  //在中间位置(max-num)
		var prevSort = $(ul).prev().find("input[name='sorts']").val();
		$(ul).find("input[name='sorts']").val(parseInt(prevSort)+5);  //默认等于上一个加5
		$(".sortable li").each(function(index){
			if(index>(max-num-1)){ //后面的每个自动加5
				var sortObj = $(this).find("input[name='sorts']");
				sortObj.val(parseInt(sortObj.val())+5);
			}
		});
	}
}

function showMessageBox(text){
	$("#messageBox").html(text);
	$("#messageBox").show();
}

function hideMessageBox(){
	if(!$("#messageBox").is(":hidden")){
		$("#messageBox").animate({opacity: "hide"}, 3000);
	}
	if(!$(".message").is(":hidden")){
		$(".message").animate({opacity: "hide"}, 3000);
	}
}

function error(){
	showMessageBox("操作失败");
	hideMessageBox();
}

/**
 * 商店部分
 * @param obj
 * @param showWidth
 * @return
 */

function homePage(){
	document.location=ctxShop+"?sid="+sid+"&mobile="+mobile+"&classifyId="+classifyId+"&shopModel="+shopModel+"&appId="+appId;				
}

function initRichTextSize(obj, showWidth){
	$(obj).find("div").each(function(){
		$(this).css("max-width", showWidth).css("height", "auto");
	});
	$(obj).find("table").each(function(){
		$(this).css("max-width", showWidth).css("height", "auto");
	});
	$(obj).find("img").each(function(){
		$(this).css("max-width", showWidth).css("height", "auto");
	});
}

/**
 * 显示more菜单
 * @return
 */
function showMoreMenu(){
	$(".more-menu").slideToggle(500);
}
/**
 * 显示我的订单more菜单
 * @return
 */
function showMoreMenu2(){
	$(".more-menu2").slideToggle(500);
}

/**
 * 判断商品是否可以操作
 * @param product
 * @return
 */
function checkAddOrDiffIcon(product){
	var productId = product.id;
	if(product.number==0){ //如果该商品没有加入购物车
		//add图片变亮
		$("#"+productId).find(".add").attr("status", "1").attr("src", ctxFront+"/images/shop/add-2.png");
		//diff图片变灰
		$("#"+productId).find(".diff").attr("status", "0").attr("src", ctxFront+"/images/shop/diff-1.png");
	}else if(product.number>0){
		//diff变亮
		$("#"+productId).find(".diff").attr("status", "1").attr("src", ctxFront+"/images/shop/diff-2.png");
		if(product.purchaseNum>0){
			//如果是有限购的商品，下次+1的时候会大于限购量，添加商品的图片变成灰色
			if((product.number+1)>product.purchaseNum){
				$("#"+productId).find(".add").attr("status", "0").attr("src", ctxFront+"/images/shop/add-1.png");
				return;
			}
		}
		//add图片变亮
		$("#"+productId).find(".add").attr("status", "1").attr("src", ctxFront+"/images/shop/add-2.png");
		
	}
	
}
/**
 * 购物车中+、-图片控制
 * @param product
 * @return
 */
function checkShoppingCartAddOrDiffIcon(product){
	var productId = product.id;
	if(product.number==product.startSaleNum){
		//diff图片变灰
		$("#"+productId).find(".diff").attr("status", "0").attr("src", ctxFront+"/images/shop/diff-1.png");
	}else{
		if(product.number==0){
			//diff图片变灰
			$("#"+productId).find(".diff").attr("status", "0").attr("src", ctxFront+"/images/shop/diff-1.png");
		}else{
			//diff图片变亮
			$("#"+productId).find(".diff").attr("status", "1").attr("src", ctxFront+"/images/shop/diff-2.png");
		}
		if(product.purchaseNum>0){
			//如果是有限购的商品，下次+1的时候会大于限购量，添加商品的图片变成灰色
			if((product.number+1)>product.purchaseNum){
				$("#"+productId).find(".add").attr("status", "0").attr("src", ctxFront+"/images/shop/add-1.png");
				return;
			}
		}
		//add图片变亮
		$("#"+productId).find(".add").attr("status", "1").attr("src", ctxFront+"/images/shop/add-2.png");
	}
}
/**
 * 添加商品到购物车
 * @param productId
 * @return
 */
function addProductCart(productId){
	//验证是否登录
	if(!checkLogin()) return;
	
	var product = new Object(); //添加的当前商品对象
	var index = 0; //当前添加的是当前页面中显示的商品索引
	var addNum = 0; //当前添加的商品数量
	var amount = 0.00;
	for(var i in products){
		if(productId==products[i].id){
			product = products[i];
			index = i; break;
		}
	}
	if($("#"+productId).find(".add").attr("status")=="0"){  //如果当前图片是不可操作状态直接提示
		openPopup("抱歉，当前商品限购数量 "+product.purchaseNum+" 件，不能再高了。");
		closePopup(5000);
		return;
	}
	if(product.number==0){ //当前商品购买为0
		if(product.purchaseNum>0){
			if(product.startSaleNum>product.purchaseNum){
				openPopup("抱歉，当前商品限购数量 "+product.purchaseNum+" 件，不能再高了。");
				closePopup(5000);
				return;
			}else{
				product.number = product.startSaleNum;
				//如果是有限购的商品，下次+1的时候会大于限购量，添加商品的图片变成灰色
				if((product.number+1)>product.purchaseNum){
					$("#"+productId).find(".add").attr("status", "0").attr("src", ctxFront+"/images/shop/add-1.png");
				}
			}
		}else{
			product.number = product.startSaleNum;
		}
		addNum = product.startSaleNum;
	}else{
		if(product.purchaseNum>0){
			if((product.number+1)>product.purchaseNum){
				openPopup("抱歉，当前商品限购数量 "+product.purchaseNum+" 件，不能再高了。");
				closePopup(5000);
				return;
			}else{
				product.number = product.number+1;
				//如果是有限购的商品，下次+1的时候会大于限购量，添加商品的图片变成灰色
				if((product.number+1)>product.purchaseNum){
					$("#"+productId).find(".add").attr("status", "0").attr("src", ctxFront+"/images/shop/add-1.png");
				}
			}
		}else{
			product.number = product.number+1;
		}
		addNum = 1;
	}
	if($("#"+productId).find(".diff").attr("status")=="0"){ //如果递减的图片是灰色状态，变成可操作
		$("#"+productId).find(".diff").attr("status", "1").attr("src", ctxFront+"/images/shop/diff-2.png");
	}
	amount = product.number*product.price; 
	product.amount=changeTwoDecimal_f(amount);
	products[index]=product; //这一步很重要
	//end 商品购买数量添加完成
	//此时需要做以下几件事
	//1、改变页面上显示的该商品购买数量值
	$("#"+productId).find(".choice-num strong").text(product.number);
	//2、重新计算购物城中的总购买数量和总金额
	totalNumber += addNum;
	totalAmount = parseFloat(totalAmount)+(addNum * product.price);
	totalAmount = changeTwoDecimal_f(totalAmount);
	$(".totalAmount strong").text("￥"+totalAmount);
	if($(".totalNumber")){
		if($(".totalNumber-badger").is(":hidden")){
			$(".totalNumber-badger").show();
		}
		$(".totalNumber").text(totalNumber);
	}
	//检测是否免运费
	checkFreeAmount();
	//3、去保存或者修改购物车中该商品的购买信息
	//异步保存购买该商品信息至购物车
	var param = "id="+product.cartId+"&merchant.id="+sid+"&user.mobile="+mobile+"&product.id="+productId+"&number="+product.number+"&price="+product.price+"&amount="+product.amount;
	//异步获取更多
	$.ajax({
		type: 'get',
		url: ctxShop+'/shoppingcart/save',
		data: param,
		dataType: 'json',
		success: function(data){
			if(data.status==200){
				product.cartId=data.message;
				products[index]=product; //这一步很重要
				if(appId!="" && appId!=null){
					document.location="ievent://getShoppingCartCount";
				}
			}else{
				openPopup("操作失败");
				closePopup(2000);
			}
		},
		failure: function(){
			openPopup("操作失败");
			closePopup(2000);
		}				
	});
}

function diffProductCart(productId, type){
	//验证是否登录
	if(!checkLogin()) return;
	
	var product = new Object(); //添加的当前商品对象
	var index = 0; //当前添加的是当前页面中显示的商品索引
	var diffNum = 0; //当前添加的商品数量
	var amount = 0.00;
	for(var i in products){
		if(productId==products[i].id){
			product = products[i];
			index = i; break;
		}
	}
	if(product.number==0 || $("#"+productId).find(".diff").attr("status")=="0"){  //如果当前图片是不可操作状态直接退出
		if(type==1){
			openPopup("抱歉，当前商品限起购数量 "+product.startSaleNum+" 件，不能再低了。");
			closePopup(5000);
		}
		return;
	}
	if((product.number-1)<product.startSaleNum){ //如果在-1的情况下小雨
		product.number = product.number-product.startSaleNum;
		diffNum = product.startSaleNum;
		$("#"+productId).find(".diff").attr("status", "0").attr("src", ctxFront+"/images/shop/diff-1.png");
	}else{
		product.number = product.number-1;
		diffNum = 1;
	}
	$("#"+productId).find(".add").attr("status", "1").attr("src", ctxFront+"/images/shop/add-2.png");
	amount = product.number*product.price; 
	product.amount=changeTwoDecimal_f(amount);
	products[index]=product; //这一步很重要
	if(type==1){
		checkShoppingCartAddOrDiffIcon(product);
	}
	//end 商品购买数量添加完成
	//此时需要做以下几件事
	//1、改变页面上显示的该商品购买数量值
	$("#"+productId).find(".choice-num strong").text(product.number);
	//2、重新计算购物城中的总购买数量和总金额
	totalNumber -= diffNum;
	totalAmount = parseFloat(totalAmount)-(diffNum * product.price);
	totalAmount = changeTwoDecimal_f(totalAmount);
	$(".totalAmount strong").text("￥"+totalAmount);
	if($(".totalNumber")){
		$(".totalNumber").text(totalNumber);
		if(totalNumber==0){
			$(".totalNumber-badger").hide();
		}
	}
	//检测是否免运费
	checkFreeAmount();
	//3、去保存或者修改购物车中该商品的购买信息
	//异步保存购买该商品信息至购物车
	var param = "id="+product.cartId+"&merchant.id="+sid+"&user.mobile="+mobile+"&product.id="+productId+"&number="+product.number+"&price="+product.price+"&amount="+product.amount;
	//异步获取更多
	$.ajax({
		type: 'get',
		url: ctxShop+'/shoppingcart/save',
		data: param,
		dataType: 'json',
		success: function(data){
			if(data.status==200){
				product.cartId=data.message; //该商品添加第一件的时候返回的cartId有用，用于后再次操作使用
				products[index]=product; //这一步很重要
				if(appId!="" && appId!=null){
					document.location="ievent://getShoppingCartCount";
				}
			}else{
				openPopup("操作失败");
				closePopup(2000);
			}
		},
		failure: function(){
			openPopup("操作失败");
			closePopup(2000);
		}				
	});
}

/**
 * 商品详情页面添加商品
 * @param productId
 * @return
 */
function addProductDetailCart(productId){
	var addNum = 0; //当前添加的商品数量
	var amount = 0.00;
	if($("#"+productId).find(".add").attr("status")=="0"){  //如果当前图片是不可操作状态直接提示
		openPopup("抱歉，当前商品限购数量 "+product.purchaseNum+" 件，不能再高了。");
		closePopup(5000);
		return;
	}
	if(product.number==0){ //当前商品购买为0
		if(product.purchaseNum>0){
			if(product.startSaleNum>product.purchaseNum){
				openPopup("抱歉，当前商品限购数量 "+product.purchaseNum+" 件，不能再高了。");
				closePopup(5000);
				return;
			}else{
				product.number = product.startSaleNum;
				//如果是有限购的商品，下次+1的时候会大于限购量，添加商品的图片变成灰色
				if((product.number+1)>product.purchaseNum){
					$("#"+productId).find(".add").attr("status", "0").attr("src", ctxFront+"/images/shop/add-1.png");
				}
			}
		}else{
			product.number = product.startSaleNum;
		}
		addNum = product.startSaleNum;
	}else{
		if(product.purchaseNum>0){
			if((product.number+1)>product.purchaseNum){
				openPopup("抱歉，当前商品限购数量 "+product.purchaseNum+" 件，不能再高了。");
				closePopup(5000);
				return;
			}else{
				product.number = product.number+1;
				//如果是有限购的商品，下次+1的时候会大于限购量，添加商品的图片变成灰色
				if((product.number+1)>product.purchaseNum){
					$("#"+productId).find(".add").attr("status", "0").attr("src", ctxFront+"/images/shop/add-1.png");
				}
			}
		}else{
			product.number = product.number+1;
		}
		addNum = 1;
	}
	if($("#"+productId).find(".diff").attr("status")=="0"){ //如果递减的图片是灰色状态，变成可操作
		$("#"+productId).find(".diff").attr("status", "1").attr("src", ctxFront+"/images/shop/diff-2.png");
	}
	amount = product.number*product.price; 
	product.amount=changeTwoDecimal_f(amount);
	//end 商品购买数量添加完成
	//此时需要做以下几件事
	//1、改变页面上显示的该商品购买数量值
	$("#"+productId).find(".choice-num strong").text(product.number);
	//2、重新计算购物城中的总购买数量和总金额
	totalNumber += addNum;
	totalAmount = parseFloat(totalAmount)+(addNum * product.price);
	totalAmount = changeTwoDecimal_f(totalAmount);
	$(".totalAmount strong").text("￥"+totalAmount);
	if($(".totalNumber")){
		if($(".totalNumber-badger").is(":hidden")){
			$(".totalNumber-badger").show();
		}
		$(".totalNumber").text(totalNumber);
	}
}

/**
 * 商品详情页面递减商品
 * @param productId
 * @param type
 * @return
 */
function diffProductDetailCart(productId, type){
	var diffNum = 0; //当前添加的商品数量
	var amount = 0.00;
	if(product.number==0 || $("#"+productId).find(".diff").attr("status")=="0"){  //如果当前图片是不可操作状态直接退出
		if(type==1){
			openPopup("抱歉，当前商品限起购数量 "+product.startSaleNum+" 件，不能再低了。");
			closePopup(5000);
		}
		return;
	}
	if((product.number-1)<product.startSaleNum){ //如果在-1的情况下小雨
		product.number = product.number-product.startSaleNum;
		diffNum = product.startSaleNum;
		$("#"+productId).find(".diff").attr("status", "0").attr("src", ctxFront+"/images/shop/diff-1.png");
	}else{
		product.number = product.number-1;
		diffNum = 1;
	}
	$("#"+productId).find(".add").attr("status", "1").attr("src", ctxFront+"/images/shop/add-2.png");
	amount = product.number*product.price; 
	product.amount=changeTwoDecimal_f(amount);
	if(type==1){
		checkShoppingCartAddOrDiffIcon(product);
	}
	//end 商品购买数量添加完成
	//此时需要做以下几件事
	//1、改变页面上显示的该商品购买数量值
	$("#"+productId).find(".choice-num strong").text(product.number);
	//2、重新计算购物城中的总购买数量和总金额
	totalNumber -= diffNum;
	totalAmount = parseFloat(totalAmount)-(diffNum * product.price);
	totalAmount = changeTwoDecimal_f(totalAmount);
	$(".totalAmount strong").text("￥"+totalAmount);
	if($(".totalNumber")){
		$(".totalNumber").text(totalNumber);
		if(totalNumber==0){
			$(".totalNumber-badger").hide();
		}
	}
}

function joinShoppingCart(){
	//验证是否登录
	if(!checkLogin()) return;
	
	if(product.number < product.startSaleNum){
		openPopup("抱歉，当前商品限起购数量 "+product.startSaleNum+" 件，请添加。");
		closePopup(5000);
		return;
	}
	//异步保存购买该商品信息至购物车
	var param = "id="+product.cartId+"&merchant.id="+sid+"&user.mobile="+mobile+"&product.id="+product.id+"&number="+product.number+"&price="+product.price+"&amount="+product.amount;
	loading('正在提交，请稍等...');
	//异步获取更多
	$.ajax({
		type: 'get',
		url: ctxShop+'/shoppingcart/save',
		data: param,
		dataType: 'json',
		success: function(data){
			hideLoader();	
			if(data.status==200){
				product.cartId=data.message;
				openPopup("已成功加入购物车");
				closePopup(2000);
				if(appId!="" && appId!=null){
					document.location="ievent://getShoppingCartCount";
				}
			}else{
				openPopup("操作失败");
				closePopup(2000);
			}
		},
		failure: function(){
			openPopup("操作失败");
			closePopup(2000);
		}				
	});
}

function clearShoppingCart(cartId, amount){
	//验证是否登录
	if(!checkLogin()) return;
	if(cartId.length==0){
		$("#popupDialog").popup("close");
	}
	
	//异步保存购买该商品信息至购物车
	var param = "id="+cartId+"&merchant.id="+sid+"&user.mobile="+mobile;
	loading('正在提交，请稍等...');
	var locationUrl = window.location.href;
	var ctxUrl = ctxShop;
	if(locationUrl.indexOf("www.9ievent.com:8080")>=0){
		ctxUrl = "http://www.9ievent.com:8080/ieventvm/shops";
	}
	//异步获取更多
	$.ajax({
		type: 'get',
		url: ctxUrl+'/shoppingcart/clear',
		data: param,
		dataType: 'json',
		success: function(data){
			hideLoader();
			if(data.status==200){
				if(cartId.length>0){ //移出单个商品
					$("ul.custom-list").find("li[id='"+cartId+"']").remove(); 
					$("ul.custom-list").listview('refresh'); // 刷新listview控件
					totalAmount = parseFloat($("#totalAmount").val());
					totalAmount = changeTwoDecimal_f(totalAmount-amount);
					$("#totalAmount").val(totalAmount)
					$(".totalAmount strong").text("￥"+totalAmount);
					if($("ul.custom-list li").length==0){
						$("#wrapper").hide();
						$(".cart-empty").show();
						$(".ui-footer").hide();
					}
				}else{ //清空购物车
					$("ul.custom-list li").remove();
					$("ul.custom-list").listview('refresh'); // 刷新listview控件
					$("#wrapper").hide();
					$(".cart-empty").show();
					$(".ui-footer").hide();
				}
				//检测是否免运费
				checkFreeAmount();
				openPopup(data.message);
				closePopup(2000);
				if(appId!="" && appId!=null){
					document.location="ievent://getShoppingCartCount";
				}
			}else{
				openPopup("操作失败");
				closePopup(2000);
			}
		},
		failure: function(){
			openPopup("操作失败");
			closePopup(2000);
		}				
	});
}
//deliveryFee:派送费金额
function checkFreeAmount(){
	if("undefined" == typeof hasPrize || hasPrize || gift){
		$(".freeDiffAmount").hide();
		$(".totalAmount").css("margin-top", "0px");
	}else{
		//判断是否免运费
		if($(".freeDiffAmount").length>0){
			if(totalAmount>=$lowFee){ 
				$(".freeDiffAmount").hide();
				$(".totalAmount").css("margin-top", "0px");
			}else{
				if($fee-0 != 0){
					$(".totalAmount").css("margin-top", "-10px");
					$(".freeDiffAmount").show();
				}else{
					$(".freeDiffAmount").hide();
					$(".totalAmount").css("margin-top", "0px");
				}
				var diffAmount = changeTwoDecimal_f($lowFee-totalAmount);
				$(".freeDiffAmount strong").text(diffAmount);
			}
		}
	}
}
function checkTotalAmount(){
	if("undefined" == typeof hasPrize || hasPrize || gift){
		$(".totalAmountDesc").hide();
		$(".totalTitle").css("margin-top", "0px");
		$(".totalAmount").css("margin-top", "0px");
		$(".totalAmount strong").text("￥"+changeTwoDecimal_f(totalAmount));
	}else{
		//判断是否免运费
		if($(".totalAmountDesc").length>0){
			if(totalAmount>=$lowFee){
				$(".totalAmountDesc").hide();
				$(".totalTitle").css("margin-top", "0px");
				$(".totalAmount").css("margin-top", "0px");
				$(".totalAmount strong").text("￥"+changeTwoDecimal_f(totalAmount));
			}else{
				if($fee-0 != 0){
					$(".totalAmountDesc").show();
					$(".totalTitle").css("margin-top", "-10px");
					$(".totalAmount").css("margin-top", "-10px");
				}else{
					$(".totalTitle").css("margin-top", "0px");
					$(".totalAmount").css("margin-top", "0px");
					$(".totalAmountDesc").hide();
				}
				totalAmount = changeTwoDecimal_f(totalAmount+$fee);
				$(".totalAmount strong").text("￥"+totalAmount);
				$("#amount").val(totalAmount);
			}
		}
	}
}
function doTelephone(phone){
	//$("#phonePopup").popup("close");
	document.location="tel:"+phone;
}

function checkLogin(){
	//判断是否登录
	if(mobile=="" || mobile==null){
		//$("#checkLoginPopup").popup("open");
		document.location="ievent://showAlert?fun=iEventCheckLoginAlert()";
		return false;
	}
	return true;
}

function iEventCheckLoginAlert(){
	var result = {
		title:"系统提示",
		message:"此操作需要登录后才能进行！",
		buttons:[
			{
				title:"确定",
				fun:"toLogin()" //函数名自定义的，根据json返回的fun节点值去调用相应的js函数
			},
			{
				title:"取消",
				fun:"" //同上，并且如果这里没给函数则直接执行提示层close关闭
			}	
		]		
		
	}
	if($global_isAndroid){
		window.control.showAlert(JSON.stringify(result));
		return;
	}
	return JSON.stringify(result);
}

function toLogin(){
	document.location='ievent://login';
}